package com.league.zjxn.main

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.BarUtils
import com.league.zjxn.R
import com.league.zjxn.app.SPSConstance
import com.league.zjxn.entity.AboutData
import com.league.zjxn.entity.PersonalData
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.main.home.ContactUsActivity
import com.league.zjxn.main.home.SameDetailsActivity
import com.league.zjxn.main.mine.*
import com.league.zjxn.utils.JsonUtils
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.image.loadCircleImage
import com.module.lemlin.owner.OwnerAbstractFragment
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_main_mine.*
import java.lang.Exception

class MainMineFragment : OwnerViewModeFragment<UserViewModel>() {

    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }
    }

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(flMainMineBar)

        //设置
        ivMainMineSetting.setOnClickListener {
            MineSettingActivity.open()
        }

        tvMainMineFollow0.setOnClickListener {
            MineFollowActivity.open()
        }

        tvMainMineFollow1.setOnClickListener {
            MineFollowActivity.open()
        }

        tvMainMineFollow2.setOnClickListener {
            MineFollowActivity.open()
        }

        tvMainMineFollow3.setOnClickListener {
            MineFollowActivity.open()
        }

        ivMainMineQiyeApprove.setOnClickListener {
            MineApproveActivity.open()
        }
        //项目申请
        ivMainMineProjectApply.setOnClickListener {
            ProjectApplyActivity.open()
        }
        tvMainMineProjectApply.setOnClickListener {
            ProjectApplyActivity.open()
        }

        ivMainMineLeagueNotice.setOnClickListener {
            LeagueNoticeActivity.open()
        }

        //个人资料
        tvMainMineUserInfo.setOnClickListener {
            MineMaterialActivity.open()
        }

        //企业信息
        tvMainMineFirm.setOnClickListener {
            MineFirmInfoActivity.open()
        }

        //关于中交
        tvMainMineAbout.setOnClickListener {
            SameDetailsActivity.openAbout(title = "中交集团", "1")
        }

        //联系我们
        tvMainMineContactUs.setOnClickListener {
            ContactUsActivity.open()
        }


//        SPSConstance.userInfo?.let {
//            tvMainMineFollowEnterprise.text = it.follow_enterprise.toString()
//            tvMainMinePlanningProject.text = it.planning_project.toString()
//            tvMainMineToConstruction.text = it.to_construction.toString()
//            tvMainMineGovernmentPark.text = it.government_park.toString()
//        }
//
//        SPSConstance.userInfo?.let {
//            tvMainMineUserInfo.visibility = if (it.group_id == "2") View.VISIBLE else View.GONE
//            tvMainMineFirm.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
//            tvMainMineProjectApply.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
//            clMainMineOwner.visibility = if (it.group_id == "2") View.VISIBLE else View.GONE
//            ivMainMineJoin.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
//        }
//
//        SPSConstance.userInfo?.let {
//            tvMainMineAllianceApplyCount.text = it.alliance_apply_count.toString()
//            tvMainMinePendingApprovalCount.text = "待审批${it.pending_approval_count}家"
//            tvMainMineProjectApplyCount.text = it.project_apply_count.toString()
//            tvMainMinePlanningProjectCount.text = "新增${it.planning_project}条"
//        }

        fillData()
    }

    override fun initData() {
        viewModel.userFollowStatistics()
    }

    override fun onRefreshLoad() {
        viewModel.userFollowStatistics()
        viewModel.userInfo()
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData()
        })
        viewModel.followStatisticsLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            tvMainMineFollowEnterprise.text = data.follow_enterprise.toString()
            tvMainMinePlanningProject.text = data.planning_project.toString()
            tvMainMineToConstruction.text = data.to_construction.toString()
            tvMainMineGovernmentPark.text = data.government_park.toString()
        })
    }

    private fun fillData() {

        SPSConstance.userInfo?.let {
            tvMainMineAccount.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
            clMainMineAccount.visibility = if (it.group_id == "2") View.VISIBLE else View.GONE

            ivMainMineHeader.loadCircleImage(it.logo)

            tvMainMineAccount.text = it.name
            tvMainMineAccountOwner.text = it.name

            val personalData: PersonalData? = try {
                JsonUtils.objectToClass(it.personal_data, PersonalData::class.java)
            } catch (e: Exception) {
                null
            }
            if (personalData != null) {
                tvMainMineUserLevel.text = "${personalData.level}级管理员"
                tvMainMineUserIndustry.text = personalData.industry
            }
        }

        SPSConstance.userInfo?.let {
            tvMainMineUserInfo.visibility = if (it.group_id == "2") View.VISIBLE else View.GONE
            tvMainMineFirm.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
            tvMainMineProjectApply.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
            clMainMineOwner.visibility = if (it.group_id == "2") View.VISIBLE else View.GONE
            ivMainMineLeagueNotice.visibility = if (it.group_id == "2") View.GONE else View.VISIBLE
        }

        SPSConstance.userInfo?.let {
            tvMainMineFollowEnterprise.text = it.follow_enterprise.toString()
            tvMainMinePlanningProject.text = it.planning_project.toString()
            tvMainMineToConstruction.text = it.to_construction.toString()
            tvMainMineGovernmentPark.text = it.government_park.toString()
        }

        SPSConstance.userInfo?.let {
            tvMainMineAllianceApplyCount.text = it.alliance_apply_count.toString()
            tvMainMinePendingApprovalCount.text = "待审批${it.pending_approval_count}家"
            tvMainMineProjectApplyCount.text = it.project_apply_count.toString()
            tvMainMinePlanningProjectCount.text = "新增${it.planning_project}条"
        }

    }
}